home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / var / lib / dpkg / info / linux-image-3.2.0-4-486.postrm < prev    next >
Encoding:
Text File  |  2012-12-19  |  7.5 KB  |  273 lines

  1. #! /usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use Cwd 'abs_path';
  5.  
  6. # Debconf may not be around here.
  7. my $have_debconf = 0;
  8. my $capb;
  9.  
  10. eval {require Debconf::Client::ConfModule;};
  11. if ( ! $@ )
  12. {
  13.   $have_debconf++;
  14.   import Debconf::Client::ConfModule ':all';
  15.   version('2.0');
  16.   $capb=capb("backup");
  17. }
  18.  
  19. $|=1;
  20. # Predefined values:
  21. my $version           = "3.2.0-4-486";
  22. my $link_in_boot      = "";
  23. my $kimage            = "vmlinuz";
  24. my $initrd            = "YES";     # initrd kernel
  25. my $postrm_hook       = '';       #Normally we do not
  26. my $kernel_arch       = "x86";
  27. my $ramdisk           = "=MK";  # List of tools to create initial ram fs.
  28. my $package_name    = "linux-image-$version";
  29.  
  30. #known variables
  31. my $image_dest      = "/";
  32. my $realimageloc    = "/boot/";
  33. my $CONF_LOC        = '/etc/kernel-img.conf';
  34.  
  35. chdir('/')           or die "could not chdir to /:$!\n";
  36.  
  37.  
  38. if (-r "$CONF_LOC" && -f "$CONF_LOC"  ) {
  39.   if (open(CONF, "$CONF_LOC")) {
  40.     while (<CONF>) {
  41.       chomp;
  42.       s/\#.*$//g;
  43.       next if /^\s*$/;
  44.  
  45.       $link_in_boot    = "" if /link_in_boot\s*=\s*(no|false|0)\s*$/i;
  46.  
  47.       $link_in_boot    = "Yes" if /link_in_boot\s*=\s*(yes|true|1)\s*$/i;
  48.  
  49.       $image_dest      = "$1"  if /image_dest\s*=\s*(\S+)/i;
  50.       $postrm_hook     = "$1"  if /postrm_hook\s*=\s*(\S+)/i;
  51.     }
  52.     close CONF;
  53.   }
  54. }
  55.  
  56. if ($link_in_boot) {
  57.   $image_dest = $realimageloc;
  58. }
  59.  
  60. $image_dest = "$image_dest/";
  61. $image_dest =~ s|/+$|/|o;
  62.  
  63. # The destdir may be gone by now.
  64. if (-d "$image_dest") {
  65.   chdir("$image_dest") or die "could not chdir to $image_dest:$!\n";
  66. }
  67.  
  68. $ENV{KERNEL_ARCH}=$kernel_arch if $kernel_arch;
  69.  
  70.  
  71. ######################################################################
  72. ######################################################################
  73. ############
  74. ######################################################################
  75. ######################################################################
  76. sub remove_sym_link {
  77.   my $bad_image = $_[0];
  78.  
  79.   warn "Removing symbolic link $bad_image \n";
  80.   warn "You may need to re-run your boot loader\n";
  81.   # Remove the dangling link
  82.   unlink "$bad_image";
  83. }
  84.  
  85. ######################################################################
  86. ######################################################################
  87. ############
  88. ######################################################################
  89. ######################################################################
  90. sub CanonicalizePath {
  91.   my $path = join '/', @_;
  92.   my @work = split '/', $path;
  93.   my @out;
  94.   my $is_absolute;
  95.  
  96.   if (@work && $work[0] eq "") { $is_absolute = 1; shift @work; }
  97.  
  98.   while (@work) {
  99.     my $seg = shift @work;
  100.     if ($seg eq "." || $seg eq "") {
  101.     } elsif ($seg eq "..") {
  102.       if (@out && $out[-1] ne "..") {
  103.         pop @out;
  104.       } else {
  105.         # Leading "..", or "../..", etc.
  106.         push @out, $seg;
  107.       }
  108.     } else {
  109.       push @out, $seg;
  110.     }
  111.   }
  112.  
  113.   unshift @out, "" if $is_absolute;
  114.   return join('/', @out);
  115. }
  116.  
  117. ######################################################################
  118. ######################################################################
  119. ############
  120. ######################################################################
  121. ######################################################################
  122. # This removes dangling symlinks. What do we do about hard links? Surely a 
  123. # something with the nane $image_dest . "$kimage" ought not to be left behind? 
  124. sub image_magic {
  125.   my $kimage = $_[0];
  126.   my $image_dest = $_[1];
  127.  
  128.   if (-l "$kimage") {
  129.     # There is a symbolic link
  130.     my $force_move = 0;
  131.     my $vmlinuz_target = readlink "$kimage";
  132.     my $real_target = '';
  133.     $real_target = abs_path($vmlinuz_target) if defined ($vmlinuz_target);
  134.     if (!defined($vmlinuz_target) || ! -f "$real_target") {
  135.       # what, a dangling symlink?
  136.       warn "The link "  . $image_dest . "$kimage is a damaged link\n";
  137.       # Remove the dangling link
  138.       &remove_sym_link("$kimage");
  139.     }
  140.     else {
  141.       my $canonical_target = CanonicalizePath("$vmlinuz_target");
  142.       if (! -e $canonical_target) {
  143.     warn "The link "  . $image_dest . "$kimage is a dangling link\n";
  144.     &remove_sym_link("$kimage");
  145.       }
  146.     }
  147.   }
  148. }
  149.  
  150. # set the env var stem
  151. $ENV{'STEM'} = "linux";
  152.  
  153. sub exec_script {
  154.   my $type   = shift;
  155.   my $script = shift;
  156.   print STDERR "Running $type hook script $script.\n";
  157.   system ("$script $version $realimageloc$kimage-$version") &&
  158.     print STDERR "User $type hook script [$script] ";
  159.   if ($?) {
  160.     if ($? == -1) {
  161.       print STDERR "failed to execute: $!\n";
  162.     }
  163.     elsif ($? & 127) {
  164.       printf STDERR "died with signal %d, %s coredump\n",
  165.         ($? & 127),  ($? & 128) ? 'with' : 'without';
  166.     }
  167.     else {
  168.       printf STDERR "exited with value %d\n", $? >> 8;
  169.     }
  170.   }
  171. }
  172. sub run_hook {
  173.   my $type   = shift;
  174.   my $script = shift;
  175.   if ($script =~ m,^/,) {
  176.     # Full path provided for the hook script
  177.     if (-x "$script") {
  178.       &exec_script($type,$script);
  179.     }
  180.     else {
  181.       warn "The provided $type hook script [$script] could not be run.\n";
  182.     }
  183.   }
  184.   else {
  185.     # Look for it in a safe path
  186.     for my $path ('/bin', '/sbin', '/usr/bin', '/usr/sbin') {
  187.       if (-x "$path/$script") {
  188.         &exec_script($type, "$path/$script");
  189.         return 0;
  190.       }
  191.     }
  192.     # No luck
  193.     print STDERR "Could not find $type hook script [$script].\n";
  194.     warn "Looked in: '/bin', '/sbin', '/usr/bin', '/usr/sbin'\n";
  195.   }
  196. }
  197.  
  198. my $options;
  199. for (@ARGV) {
  200.     s,','\\'',g;
  201.     $options .= " '$_'";
  202. }
  203. $ENV{'DEB_MAINT_PARAMS'}="$options";
  204.  
  205. ## Run user hook script here, if any
  206. if ($postrm_hook) {
  207.   &run_hook("postrm", $postrm_hook);
  208. }
  209. if (-d "/etc/kernel/postrm.d") {
  210.   warn "Examining /etc/kernel/postrm.d .\n";
  211.   system ("run-parts --verbose --exit-on-error --arg=$version " .
  212.           "--arg=$realimageloc$kimage-$version " .
  213.           "/etc/kernel/postrm.d") &&
  214.             die "Failed to process /etc/kernel/postrm.d";
  215. }
  216. if (-d "/etc/kernel/postrm.d/$version") {
  217.   warn "Examining /etc/kernel/postrm.d/$version .\n";
  218.   system ("run-parts --verbose --exit-on-error --arg=$version " .
  219.           "--arg=$realimageloc$kimage-$version " .
  220.           "/etc/kernel/postrm.d/$version") &&
  221.             die "Failed to process /etc/kernel/postrm.d/$version";
  222. }
  223.  
  224. # purge initramfs and related
  225. if ($ARGV[0] !~ /upgrade/) {
  226.   if (-f $realimageloc . "initrd.img-$version") {
  227.     unlink $realimageloc . "initrd.img-$version";
  228.   }
  229.   if (-f $realimageloc . "initrd.img-$version.bak") {
  230.     unlink $realimageloc . "initrd.img-$version.bak";
  231.   }
  232.   if (-f "/var/lib/initramfs-tools/$version") {
  233.     unlink "/var/lib/initramfs-tools/$version";
  234.   }
  235.   # check and remove damaged and dangling symlinks
  236.   image_magic($kimage,          $image_dest);
  237.   image_magic($kimage . ".old", $image_dest);
  238.   image_magic("initrd.img",     $image_dest) if $initrd;
  239.   image_magic("initrd.img.old", $image_dest) if $initrd;
  240. }
  241.  
  242.  
  243. # Ignore all invocations except when called on to purge.
  244. exit 0 unless $ARGV[0] =~ /purge/;
  245.  
  246. my $ret = purge();
  247.  
  248. my @files_to_remove = qw{
  249.                          modules.dep modules.isapnpmap modules.pcimap
  250.                          modules.usbmap modules.parportmap
  251.                          modules.generic_string modules.ieee1394map
  252.                          modules.ieee1394map modules.pnpbiosmap
  253.                          modules.alias modules.ccwmap modules.inputmap
  254.                          modules.symbols modules.ofmap
  255.                          modules.seriomap modules.*.bin
  256.              modules.softdep modules.devname
  257.                        };
  258.  
  259. foreach my $extra_file (@files_to_remove) {
  260.   for (glob("/lib/modules/$version/$extra_file")) {
  261.     unlink;
  262.   }
  263. }
  264.  
  265. if (-d "/lib/modules/$version" ) {
  266.   system ("rmdir", "/lib/modules/$version");
  267. }
  268.  
  269. exit 0;
  270.  
  271. __END__
  272.